package com.acceptable.qucun.file.aspect;

import com.acceptable.qucun.file.entity.FileShare;
import com.acceptable.qucun.file.entity.ShareInfo;
import com.acceptable.qucun.file.entity.template.TemplateFile;
import com.acceptable.qucun.file.exception.FileOperationException;
import com.acceptable.qucun.file.mapper.FileShareItemMapper;
import com.acceptable.qucun.file.mapper.FileShareMapper;
import com.acceptable.qucun.file.mapper.TemplateFileMapper;
import com.acceptable.qucun.file.service.impl.FileDatabaseOperation;
import com.acceptable.qucun.generic.exception.GenericExcetion;
import com.acceptable.qucun.generic.util.AspectUtil;
import com.acceptable.qucun.generic.util.CodeUtil;
import com.acceptable.qucun.generic.util.StringFormatUtil;
import com.acceptable.qucun.generic.util.result.error.FileErrorResult;
import com.acceptable.qucun.generic.util.result.error.GenericErrorResult;
import com.acceptable.qucun.user.entity.User;
import com.acceptable.qucun.user.mapper.UserMapper;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Aspect
@Component
public class ShareAspect {
    @Autowired private FileShareItemMapper shareItemMapper;
    @Autowired private TemplateFileMapper fileMapper;
    @Autowired private FileShareMapper shareMapper;
    @Autowired private UserMapper userMapper;
    @Autowired private FileDatabaseOperation fileDatabaseOperation;

    @Pointcut("execution(* com.acceptable.qucun.file.service.FileShareService.*SharedFile*(..))")
    public void sharedFile(){}

    @Pointcut("execution(* com.acceptable.qucun.file.service.FileShareService.*SharedInfo*(..))")
    public void sharedInfo(){}

    @Before(value = "sharedFile()")
    public void sharedFileExist(JoinPoint joinPoint){
        Map<String, String> dataMap = (Map<String, String>) AspectUtil.paramMap(joinPoint).get("HashMap");
        for (Map.Entry<String, String> entry: dataMap.entrySet()){
            System.out.println(entry.getKey() + " " + entry.getValue());
        }

        Integer srcUserId = Integer.parseInt(dataMap.get("src_user_id"));

        User user = userMapper.selectByUserId(srcUserId);
        if (user == null || user.getDeleted())
            throw new FileOperationException(FileErrorResult.SHARE_NOT_EXIST_ERROR);

        List<Long> fileIdList = CodeUtil.string2List(dataMap.get("file_id"), Long.class);
//        int size = shareItemMapper.selectBatchByFileId(srcUserId, fileIdList).size();
        // 可能被分享文件本身 也可能是被分享文件的子文件
        String shareIdStr = dataMap.get("share_id");
        if (StringFormatUtil.isEmpty(shareIdStr))
            throw new GenericExcetion(GenericErrorResult.DATA_NOT_EXIST_ERROR);

        Integer shareId = Integer.parseInt(shareIdStr);
        List<TemplateFile> fileList = fileMapper.selectByShareId(srcUserId, shareId);

        if (fileList.size() == 0){
            throw new FileOperationException(FileErrorResult.NOT_EXIST_ERROR);
        }

        List<TemplateFile> shareFileList = new ArrayList<>();
        for (TemplateFile file: fileList){
            if (file.getDict()) {
                shareFileList.addAll(fileDatabaseOperation.selectSubFileList(srcUserId, file.getFileId()));
            }
            shareFileList.add(file);
        }

        System.out.println("saveIdList: " + fileIdList);
        int count = 0;
        for (TemplateFile file: shareFileList){
            if (fileIdList.contains(file.getFileId()))
                count ++;
        }

        if (count != fileIdList.size())
            throw new FileOperationException(FileErrorResult.SHARE_NOT_EXIST_ERROR);
    }

    @Before(value = "sharedInfo()")
    public void sharedInfoExist(JoinPoint joinPoint){
        Map<String, String> dataMap = (Map<String, String>) AspectUtil.paramMap(joinPoint).get("HashMap");
//        Integer userId = Integer.parseInt(dataMap.get("src_user_id"));0
        Integer shareId = Integer.parseInt(dataMap.get("share_id"));
        FileShare share = shareMapper.selectById(shareId);
        if (share == null)
            throw new FileOperationException(FileErrorResult.SHARE_NOT_EXIST_ERROR);
        else{
            ShareInfo shareInfo = new ShareInfo();
            shareInfo.setPartFieldByFileShare(share);
//            System.out.println(shareInfo);
            dataMap.put("share_info", CodeUtil.object2String(shareInfo));
        }
    }
}
